package com.example.lcode.backtrack;

import java.util.*;

public class Question491 {

    public List<List<Integer>> findSubsequences(int[] nums) {
        List<List<Integer>> result = new ArrayList<>();
        List<Integer> subRes = new ArrayList<>();
        int beginIndex = 0;
        findSubsequences(nums, result, subRes, beginIndex);
        return result;
    }

    private void findSubsequences(int[] nums, List<List<Integer>> result, List<Integer> subRes, int beginIndex) {
        if (subRes.size() >= 2 && subRes.get(subRes.size() - 1) >= subRes.get(subRes.size() - 2)) {
            result.add(new ArrayList<>(subRes));
        } else if (subRes.size() >= 2 && subRes.get(subRes.size() - 1) < subRes.get(subRes.size() - 2)){
            return;
        }
        Set<Integer> sign = new HashSet<>();
        for (int i = beginIndex; i <= nums.length - 1; i++) {
            if (sign.contains(nums[i])) {
                continue;
            } else {
                sign.add(nums[i]);
            }
            if (i != 0 && i != beginIndex && nums[i] == nums[i - 1]){
                continue;
            }
            subRes.add(nums[i]);
            findSubsequences(nums, result, subRes, i + 1);
            subRes.remove(subRes.size() - 1);
        }
    }

    public static void main(String[] args) {
        Question491 question491 = new Question491();
        List<List<Integer>> subsequences = question491.findSubsequences(new int[]{-100,-100,0,0,0,100,100,0,0});
        System.out.println(subsequences);
    }

}
